<?
Class ProductModel extends  CI_Model {
	private $table;
	function __construct() {
		$this -> table = "material_data";
		parent::__construct();
	}

	function getAllCountProduct($search) {
		$this -> whereData($search);
		$this -> db -> from($this -> table);
		return $this -> db -> count_all_results();
		

	}
	function getMainGroup(){
		$sql = "select MAIN_GROUP from material_data where main_group!='' and main_group is not null group by MAIN_GROUP;";
		$query = $this->db->query($sql);
		return $query -> result();
	}
	function searchProduct($search) {
		$this -> db -> select("*");
		$this->db->like('MAKTX', $search);
		$this->db->or_like('MATNR', $search);
		
		$this -> db -> limit(30, 0);
		$query = $this -> db -> get($this -> table);
		
		return $query -> result();
	}
	function searchProductPrice($search,$column,$perUnit) {
		$sql = "SELECT * FROM material_data WHERE $column > 0 AND $perUnit !='' AND EA_PACK  > 0 AND (`MAKTX` LIKE '%".$search."%' OR `MATNR` LIKE '%".$search."%') LIMIT 30";
		$query = $this->db->query($sql);
		return $query -> result();
	}
	function searchProductPromotion($search,$column,$promotionID) {
		$sql = "SELECT * FROM material_data WHERE  $column > 0 AND EA_PACK  > 0 AND (`MAKTX` LIKE '%".$search."%' OR `MATNR` LIKE '%".$search."%')  and MATNR IN(select MATNR FROM promotion_fmat where PROMO_ID= '$promotionID')";
		$query = $this->db->query($sql);
		return $query -> result();
	}
	function getAProduct($pID) {
		$this -> db -> select("*");
		$this->db->where("MATNR",$pID);
		$query = $this -> db -> get($this -> table);
		if($query->num_rows() > 0 ){
			return $query->row(0);
		}
		return false;
	}
	function getProduct($start, $limit, $search) {
		$this -> db -> select("*");
		$this -> whereData($search);
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get($this -> table);

		return $query -> result();
	}

	function whereData($search) {
		
		
		if(empty($search)){
			return ;
		}
		if (isset($search['matnr']) &&  $search['matnr'] != "") {
			$this -> db -> like("matnr", $search['matnr']);
		}
		if (isset($search['maktx']) && $search['maktx'] != "") {
			$this -> db -> like("maktx", $search['maktx']);
		}
		if (isset($search['matgrp']) && $search['matgrp'] != "") {
			$this -> db -> like("matgrp", $search['matgrp']);
		}
		if (isset($search['MAIN_GROUP']) && $search['MAIN_GROUP'] != "") {
			$this -> db -> like("MAIN_GROUP", $search['MAIN_GROUP']);
		}
	}

}
?>